package com.etsoft.comm.generate;

import com.etsoft.comm.tool.*;

import java.sql.ResultSet;

public class Render0_Entity {

	public static void create(GContext ctx) throws Exception {
		ResultSet l_rset = ctx.getL_rset();
		boolean l_date = ctx.is_date_exist();
		// 读取 sample 模板
		String fileStr = ctx.getSampleStr(SampleFile.Model);
		
		// ★ 开始拼字符串
		// delete flag
		if (ctx.is_deleted_exist()) {
			fileStr = fileStr.replaceFirst("##importJsonIgnore#", "import com.fasterxml.jackson.annotation.JsonIgnore;");
		} else {
			fileStr = fileStr.replaceFirst("##importJsonIgnore#" + System.lineSeparator(), "");
		}

		// 是否导入日期包
		if (l_date) {
			fileStr = fileStr.replaceFirst("##importDate#", "import java.util.Date;" + System.lineSeparator() +
					"import org.springframework.format.annotation.DateTimeFormat;" + System.lineSeparator() +
					"import com.fasterxml.jackson.annotation.JsonFormat;"
			);
		} else {
			fileStr = fileStr.replaceFirst("##importDate#" + System.lineSeparator(), "");
		}

		fileStr = fileStr.replace("##project_name#", ctx.project_name);

		// 换 表名
		fileStr = fileStr.replaceFirst("##table_name#", ctx.getTable_name());
		// 换 类名
		fileStr = fileStr.replace("##ModelClassName#", ctx.getClassNameSuffix(SampleFile.Model));

		// 变量定义，不设默认值
		MyStringBuilder sb = new MyStringBuilder();
		for (int i = 1; i <= l_rset.getMetaData().getColumnCount(); i++) {
			String col_name = l_rset.getMetaData().getColumnName(i).toLowerCase();
			// if (GContext.defaultList.contains(col_name)) {
			// 	continue; // 这些封装在父类里了
			// }
			String colName = StringUnit.getCamelName(col_name, false);
			String ColName = StringUnit.getCamelName(col_name, true);

			// sb.appendLine("\t@ApiModelProperty(value = \""+ ColName +"\")");
			// sb.appendLine("\t@TableField(\"`"+ col_name +"`\")");
			if (TypeHelper.isDateType(l_rset.getMetaData(), i)) { // cur field is date type
				sb.appendLine("\t@JsonFormat(timezone = \"GMT+8\", pattern = \"yyyy-MM-dd\") // HH:mm:ss");
				// sb.appendLine("\t@DateTimeFormat(pattern = \"yyyy-MM-dd\")");
			}
			if (ctx.deleteFlagColumn.equals(col_name)) {
				sb.appendLine("\t@JsonIgnore");
			}
			sb.append("\tprivate ");
			sb.append(TypeHelper.getTypeFromDb2Java(l_rset.getMetaData(), i));
			if (ctx.deleteFlagColumn.equals(col_name)) {
				sb.append(" " + colName + " = 0;" + System.lineSeparator());
			} else {
				sb.append(" " + colName + ";" + System.lineSeparator());
			}
		}
		fileStr = fileStr.replaceFirst("    ##Fields#", sb.substring(0, sb.length() - 1));

		// 输出内容
		FileHelper.writeFile(ctx.getFileFullPath(SampleFile.Model), ctx.getTable_name(), fileStr);
	}
}
